package cn.wolfcode.mp;

import cn.wolfcode.mp.domain.Employee;
import cn.wolfcode.mp.mapper.EmployeeMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.minidev.json.writer.UpdaterMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class generalMapper {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Test
    public void testSave(){
        Employee employee = new Employee();
        employee.setAdmin(1);
        employee.setAge(18);
        employee.setDeptId(1L);
        employee.setEmail("dafei@wolfcode");
        employee.setName("zhongfei");
        employee.setPassword("111");
        employeeMapper.insert(employee);
    }

    //需求： 将id=1用户名字修改为dafei
    @Test
    public void testUpdateById(){
        Employee employee = new Employee();
        employee.setId(1L);
        employee.setName("dafei");
        employeeMapper.updateById(employee);
    }
    //注意： 拼接sql时，所有非null 字段都进行set 拼接

    //打印sql： UPDATE employee SET name=?, age=?, admin=? WHERE id=?

    //需求： 将id=1用户名字修改为dafei
    @Test
    public void testUpdate1(){
        //先查询,再修改,再更新
        Employee employee = employeeMapper.selectById(1L);
        employee.setName("dafei");
        employee.setId(1L);
        employeeMapper.updateById(employee);
    }

    //需求： 将id=1用户名字修改为dafei
    @Test
    public void testUpdate2(){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        //updateWrapper.set("name","zhangfei");//两个参数第一个参数列名,第二个值 UPDATE employee SET name=? WHERE (id = ?)
        updateWrapper.eq("id",1L);//这个和上面一样,eq相当于拼接判断条件
        updateWrapper.setSql("name='dafei'");//sql片段,拼接sql
        //Unknown column 'dafei' in 'field list'注意string类型一定要加上单引号,要不然拼接时mp会当成一个列
        employeeMapper.update(null,updateWrapper);
    }

    @Test
    public void testDelete1(){
        //需求：删除id=1的员工信息
        employeeMapper.deleteById(1L);
        //DELETE FROM employee WHERE id=?
    }

    @Test
    public void testDelete2(){
        //需求：删除id=2， id=3的员工信息
        employeeMapper.deleteBatchIds(Arrays.asList(2L,3L));
        //DELETE FROM employee WHERE id IN ( ? , ? )
    }

    @Test
    public void testDelete3(){
        //需求：删除name=dafei并且age=18的员工信息
        //key:条件列   value:条件值
        Map<String, Object> map = new HashMap<>();
        map.put("name","dafei");//两个参数第一个参数列名,第二个值
        map.put("age",18);//两个参数第一个参数列名,第二个值
        employeeMapper.deleteByMap(map);//map相当于是添加where条件
        //DELETE FROM employee WHERE name = ? AND age = ?
    }

    @Test
    public void testDelete4(){
        //需求：需求：删除name=dafei并且age=18的员工信息
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("name","dafei");//两个参数第一个参数列名,第二个值
        wrapper.eq("age",18);//eq相当于where,mp中连用中间默认是and
        employeeMapper.delete(wrapper);
        //DELETE FROM employee WHERE (name = ? AND age = ?)
    }

    //需求：查询id=1的员工信息
    @Test
    public void testSelectById(){
        System.out.println(employeeMapper.selectById(1L));
    }
    //SELECT id,name,password,email,age,admin,dept_id FROM employee WHERE id=?

    //需求：查询id=1，id=2的员工信息
    @Test
    public void testSelectBatchIds(){
        employeeMapper.selectBatchIds(Arrays.asList(1L,2L));
    }
    //SELECT id,name,password,email,age,admin,dept_id FROM employee WHERE id IN ( ? , ? )

    //需求：查询name=dafei， age=18的员工信息
    @Test
    public void testSelectByMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("name","dafei");
        map.put("age",18);
        employeeMapper.selectByMap(map);
    }
    //SELECT id,name,password,email,age,admin,dept_id FROM employee WHERE name = ? AND age = ?


    //需求： 查询满足条件的所有的员工个数
    @Test
    public void testSelectCount() {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //employeeMapper.selectCount(null);
        //SELECT COUNT( 1 ) FROM employee
        employeeMapper.selectCount(wrapper);//上面查询和这一个意思,没有条件就直接传wrapper或者null都可以
    }

    //需求： 查询满足条件的所有的员工信息， 返回List<Employee>
    @Test
    public void testSelectList(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        employeeMapper.selectList(null);
        //SELECT id,name,password,email,age,admin,dept_id FROM employee
        employeeMapper.selectList(wrapper);//上面查询和这一个意思,没有条件就直接传wrapper或者null都可以
    }

    //需求： 查询满足条件的所有的员工信息， 返回List<Map<String, Object>>  底层将每条数据封装成HashMap
    @Test
    public void testSelectMap() {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        List<Map<String, Object>> map = employeeMapper.selectMaps(wrapper);
        map.forEach(System.out::println);
        //没有指定条件， 等价于传入null
        //List<Map<String, Object>> map2= employeeMapper.selectMaps(null);
    }

    //步骤2：编写分页代码
    @Test
    public void testSelectPage(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //参数1：当前页， 参数2：每页显示条数
        IPage<Employee> page = new Page<>(2, 3);
        employeeMapper.selectPage(page, wrapper);
        System.out.println("当前页：" + page.getCurrent());
        System.out.println("每页显示条数：" + page.getSize());
        System.out.println("总页数：" + page.getPages());
        System.out.println("总数：" + page.getTotal());
        System.out.println("当前页数据：" + page.getRecords());
    }
}
